package com.jk.lyt.service;

import com.jk.lyt.dao.UserDao;
import com.jk.lyt.pojo.UserBean;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.jk.lyt.util.PejeController.md5;

@Service
public class UserService {
    @Autowired
    private UserDao userDao;


    public String log(UserBean bean) {
        UserBean userbean = userDao.selectToId(bean.getUsername());
        if (userbean == null){
            return "账号不存在请注册";
        }

        String md5HexStr = md5(bean.getPassword());
        Base64 base64 = new Base64();
        String base64Info = base64.encodeToString(md5HexStr.getBytes());

        if (!userbean.getPassword().equals(base64Info)){
            return "密码错误";
        }
        if (userbean.getUsertype()!=2){
            return "不是管理员";
        }


        return "登录成功";
    }

    public void Register(UserBean bean) {
        String md5HexStr = md5(bean.getPassword());
        Base64 base64 = new Base64();
        String base64Info = base64.encodeToString(md5HexStr.getBytes());
        bean.setPassword(base64Info);
        userDao.Register(bean);

    }
}
